<template>
  <div id="admin">
    <div class="pos" v-loading="loading">
      <h1 class="adminh1">用户登录</h1>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名:" prop="userid">
          <el-input v-model="ruleForm.userid" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" clearable></el-input>
        </el-form-item>
        <div class="but-c">
          <el-form-item >
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </div>
        <el-divider class="line-t"></el-divider>
      </el-form>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (value.length < 8 || value.length > 13) {
              callback(new Error("长度必须在8-12之内!"));
          } else{
            callback();
          }
        }
      };
      var checkUserid = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('用户名不能为空'));
        } else {
          callback();
        }
      };
      return {
        loading: false,
        ruleForm: {
          userid: '',
          checkPass: ''
        },
        rules: {
          userid: [
            { validator: checkUserid, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
          this.loading = true;
          // this.$router.push("/menu");
          let formData = new FormData();
          formData.append('userTel',this.ruleForm.userid);
          formData.append('userPwd',this.ruleForm.checkPass);
          let url = 'http://localhost:8088/login';
          console.log(formData.get('userid'));
          this.$ajax.post(url,formData).then(resp=>{
            alert("登录成功");
                this.$router.push("/menu");
          }).catch(function (error) { // 请求失败处理
            alert("登录失败");
          });
          setTimeout(() => {
            this.loading = false;
          }, 2000);
        } else {
          console.log("error submit!!");
          return false;
        }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style scoped>
.adminh1 {
  margin: 20px 0;
  text-shadow: 10px 13px 3px rgb(207, 207, 207);
  text-align: center;
}
.pos {
  width: 80%;
  height: 40%;
  position: absolute;
  top: 25px;
  left: 25px;
}

#admin {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -200px;
  margin-left: -250px;
  width: 500px;
  height: 400px;
  background: #fff;
  border-radius: 10%;
  box-shadow: 8px 10px 10px rgb(177, 223, 242);
}
.line-t{
  top: 15px;
}
</style>